/*
 * MIT License
 *
 * Copyright (c) 2025 Mark·虎
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package com.hsxxherp.key.uia.controller;

import com.hsxxherp.key.uia.entity.SysApplication;
import com.hsxxherp.key.uia.service.ISysApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 应用控制器
 *
 * @author Mark·虎（2486970363@qq.com）
 */
@RestController
@RequestMapping("/api/applications")
public class SysApplicationController {

    @Autowired
    private ISysApplicationService applicationService;

    /**
     * 处理应用的增删改操作
     *
     * @param requestData 包含操作类型和应用信息的请求数据
     * @return 操作结果的响应实体
     */
    @PostMapping
    public ResponseEntity<?> handleApplicationOperation(@RequestBody Map<String, Object> requestData) {
        String operation = (String) requestData.get("operation");
        if (operation == null) {
            return new ResponseEntity<>("操作类型未指定", HttpStatus.BAD_REQUEST);
        }

        try {
            switch (operation) {
                case "create":
                    SysApplication createApp = new SysApplication();
                    createApp.setAppName((String) requestData.get("appName"));
                    createApp.setAppType((String) requestData.get("appType"));
                    createApp.setGroupId((String) requestData.get("groupId"));
                    // 其他属性赋值...
                    SysApplication createdApp = applicationService.insertOrCreateApplication(createApp);
                    return new ResponseEntity<>(createdApp, HttpStatus.CREATED);
                case "update":
                    SysApplication updateApp = new SysApplication();
                    updateApp.setAppId((String) requestData.get("appId"));
                    updateApp.setAppName((String) requestData.get("appName"));
                    updateApp.setAppType((String) requestData.get("appType"));
                    updateApp.setGroupId((String) requestData.get("groupId"));
                    // 其他属性赋值...
                    SysApplication updatedApp = applicationService.updateApplication(updateApp);
                    return new ResponseEntity<>(updatedApp, HttpStatus.OK);
                case "delete":
                    String appId = (String) requestData.get("appId");
                    boolean deleted = applicationService.deleteApplication(appId);
                    if (deleted) {
                        return new ResponseEntity<>("应用删除成功", HttpStatus.OK);
                    } else {
                        return new ResponseEntity<>("应用删除失败", HttpStatus.INTERNAL_SERVER_ERROR);
                    }
                default:
                    return new ResponseEntity<>("不支持的操作类型", HttpStatus.BAD_REQUEST);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}